package visualization.components;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;


/**
 * A Color Key that associates a String with a color. This should be paired with a color graph of
 * some sort.
 * 
 * @author Ben Schwab
 * 
 */
public class ColorKey {

    private static final int KEY_ENTRY_HEIGHT = 50;
    private static final int KEY_ENTRY_WIDTH = 250;
    private static final int KEY_ENTRY_BUFFER = 10;
    private ArrayList<KeyEntry> myKeyEntries = new ArrayList<KeyEntry>();
    private int myNumberOfRows;
    private Rectangle myArea;

    /**
     * 
     * @param keyArea Area of the screen to draw they key
     * @param keyInfo List of the string labels for the key
     * @param colors a color area in the same order as the key labels with matching colors
     */
    public ColorKey (Rectangle keyArea, List<String> keyInfo, Color[] colors) {
        myNumberOfRows = (int) (keyArea.getHeight() / (KEY_ENTRY_HEIGHT + KEY_ENTRY_BUFFER));
        myArea = keyArea;
        createKeyEntries(keyInfo, colors);
    }

    private void createKeyEntries (List<String> keyInfo, Color[] colors) {
        int pos = 0;
        int rowCounter = 0;
        int xPos = (int) myArea.getMinX();
        int yPos = (int) myArea.getMinY();
        int startY = yPos;
        for (String info : keyInfo) {
            Rectangle keyEntryArea = new Rectangle(xPos, yPos, KEY_ENTRY_WIDTH, KEY_ENTRY_HEIGHT);
            myKeyEntries.add(new KeyEntry(info, colors[pos], keyEntryArea));
            if (rowCounter == myNumberOfRows - 1) {
                rowCounter = 0;
                yPos = startY;
                xPos += KEY_ENTRY_WIDTH + KEY_ENTRY_BUFFER;

            }
            else {
                rowCounter++;
                yPos += KEY_ENTRY_HEIGHT + KEY_ENTRY_BUFFER;
            }
            pos++;
        }
    }

    /**
     * Paint the color key to the current graph
     * 
     * @param pen current graphics pen
     */
    public void paint (Graphics2D pen) {
        for (KeyEntry keyEntry : myKeyEntries) {
            keyEntry.paint(pen);
        }
    }
/**
 * An individual key entry
 * @author Ben Schwab
 *
 */
    private class KeyEntry {
        private static final double TEXT_MARGIN = .6;
        private static final double SQUARE_MARGIN_LEFT = .1;
        private static final double SQUARE_MARGIN = 1 - (TEXT_MARGIN + SQUARE_MARGIN_LEFT);
        private TextBox myText;
        private Color myColor;
        private Rectangle myColorSquareArea;
        private Rectangle myTextArea;

        public KeyEntry (String title, Color color, Rectangle keyPosition) {
            myTextArea =
                    new Rectangle((int) keyPosition.getMinX(), (int) keyPosition.getMinY(),
                                  (int) (keyPosition.getWidth() * TEXT_MARGIN),
                                  (int) (keyPosition.getHeight()));
            myColorSquareArea =
                    new Rectangle((int) (myTextArea.getMaxX() + SQUARE_MARGIN_LEFT *
                                                                 keyPosition.getWidth()),
                                  (int) keyPosition.getMinY(),
                                  (int) (keyPosition.getWidth() * SQUARE_MARGIN),
                                  (int) (keyPosition.getWidth() * SQUARE_MARGIN));
            myText = new TextBox(myTextArea, title);
            myColor = color;
        }

        public void paint (Graphics2D pen) {
            pen.setColor(Color.BLACK);
            myText.paint(pen);
            pen.setColor(myColor);
            pen.fillRect((int) myColorSquareArea.getMinX(), (int) myColorSquareArea.getMinY(),
                         (int) myColorSquareArea.getWidth(), (int) myColorSquareArea.getHeight());

        }
    }

}
